﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;


namespace ProjectNocturnalFool
{
    class TextFieldControl : UserControl
    {
        private List<string> messages = new List<string>();
        
        private SpriteFont font;

        private int minRecord = 0;

        //HACK hardcode
        private int returnHeight = 10;

        /// <summary>
        /// Loads Content of button
        /// </summary>
        public override void LoadContent()
        {
            base.LoadContent();

            bgTexture = TacticsGame.Instance.Content.Load<Texture2D>("Menus/Buttons/Button");
            font = TacticsGame.Instance.gameFont;

            width = 100;
            height = 100;
            isDraggable = true;
        }
        
        /// <summary>
        /// Updates logic based on mouse
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            string message;

            TacticsGame.Instance.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);

            Rectangle targetRect = new Rectangle(x, y, width, height);
            Vector2 something = new Vector2(x, y);
            if (Selected)
            {
                TacticsGame.Instance.spriteBatch.Draw(bgTexture, targetRect, null, Color.Goldenrod, 0, Vector2.Zero, SpriteEffects.None, 0);
            }
            else
            {
                TacticsGame.Instance.spriteBatch.Draw(bgTexture, targetRect, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0);                
            }

            for(int i = 0; i < messages.Count - minRecord ; i ++)
            {
                message = messages[minRecord + i];
                Vector2 size = font.MeasureString(message);
                if (returnHeight * i + y + 5 + (size.Y * 2) < width)
                {
                    TacticsGame.Instance.spriteBatch.DrawString(font, message, new Vector2(x + 5, (returnHeight * i ) + y + 5), Color.Black, 0.0f, new Vector2(0, 0), 1f, SpriteEffects.None, 0);
                }
            }
            TacticsGame.Instance.spriteBatch.End();
        }

        public void addMessage(string message)
        {
            messages.Add(message);
           Vector2 size = font.MeasureString(message);

           if (returnHeight * messages.Count  + y + 5 + (size.Y * 2) > width)
           {
               minRecord++;
           }

        }
    

    }
}
